class_name Projectile extends Node2D


@export var speed: float = 600.0
var target: Node2D


@onready var _enemy_detection_area: Area2D = $EnemyDetectionArea as Area2D


func _ready() -> void:
	if not multiplayer.is_server():
		set_physics_process(false)
		_enemy_detection_area.monitoring = false


func _physics_process(delta: float) -> void:
	if not is_instance_valid(target):
		queue_free()
	else:
		global_position = global_position.move_toward(target.global_position, speed * delta)
		look_at(target.global_position)

func _on_enemy_detection_area_body_entered(body: Node2D) -> void:
	var enemy: Enemy = body as Enemy
	if is_instance_valid(enemy):
		enemy.get_hit()
	queue_free()
